var xmlhttp;
var savedRecommandId;
var savedTitle;
var savedStatus;


function getXmlHttpObject()
{
	if (window.XMLHttpRequest)
	  {// code for IE7+, Firefox, Chrome, Opera, Safari
	  xmlhttp=new XMLHttpRequest();
	  }
	else
	  {// code for IE6, IE5
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	return xmlhttp;
}

function addRecommand()
{
	getXmlHttpObject();
	var title = document.getElementById("title").value;
	var status = document.getElementById("status").value;
	
	var url = "../controllers/addRecommand.php";
	var params = "title=" + title + "&status=" + status; 
	xmlhttp.open("POST",url,true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlhttp.onreadystatechange = addRecommandSuccess;
	xmlhttp.send(params);
}

function addRecommandSuccess()
{
	if (xmlhttp.readyState == 4)
		if (xmlhttp.status == 200)
		{
			location.reload();
		}
	    else if (request.status == 404)
	        alert("请求的链接不存在");
	    else
	        alert("错误: 错误代码 " + xmlhttp.status);
}

function deleteRecommand(recommandId)
{
	getXmlHttpObject();
	var url = "../controllers/deleteRecommand.php";
	var params = "recommandId=" + recommandId; 
	xmlhttp.open("POST",url,true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlhttp.onreadystatechange = deleteRecommandSuccess;
	xmlhttp.send(params);
}

function deleteRecommandSuccess()
{
	if (xmlhttp.readyState == 4)
		if (xmlhttp.status == 200)
		{
			location.reload();
		}
	    else if (request.status == 404)
	        alert("请求的链接不存在");
	    else
	        alert("错误: 错误代码 " + xmlhttp.status);
}

function gotoUpdateRecommand(recommandId,title,status)
{
	
	// refreshPage(recommandId,title,status);
	var requestUrl = "../views/toefl_recommand_update.php?recommandId=" + recommandId + "&title=" + title + "&status=" + status;
	window.location.href=requestUrl;
}

function addSetToRecommand(recommandId)
{
	getXmlHttpObject();
	var setId = document.getElementById("addingSet").value;
	var isImportant = document.getElementById("isImportant").value;

	var url = "../controllers/addSetToRecommand.php";
	var params = "recommandId=" + recommandId + "&setId=" + setId + "&remark=" + isImportant; 
	xmlhttp.open("POST",url,true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlhttp.onreadystatechange = addSetToRecommandSuccess;
	xmlhttp.send(params);
	
}

function addSetToRecommandSuccess()
{

	if (xmlhttp.readyState == 4)
		if (xmlhttp.status == 200)
		{
			var responseText = xmlhttp.responseText;
			responseText = responseText.replace(/\s+/g,"");
			if(responseText == "error")
			{
				alert("该机经已经存在！");
			}
			else
			{
				location.reload();
			}
		}
	    else if (request.status == 404)
	        alert("请求的链接不存在");
	    else
	        alert("错误: 错误代码 " + xmlhttp.status);
}

function deleteSetForRecommand(recommandId, setId)
{
	getXmlHttpObject();

	var url = "../controllers/deleteSetForRecommand.php";
	var params = "recommandId=" + recommandId + "&setId=" + setId; 
	xmlhttp.open("POST",url,true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlhttp.onreadystatechange = deleteSetForRecommandSuccess;
	xmlhttp.send(params);
}

function deleteSetForRecommandSuccess()
{

	if (xmlhttp.readyState == 4)
		if (xmlhttp.status == 200)
		{
			location.reload();
		}
	    else if (request.status == 404)
	        alert("请求的链接不存在");
	    else
	        alert("错误: 错误代码 " + xmlhttp.status);
}

function updateRecommandInfo(recommandId)
{
	getXmlHttpObject();
	var title = document.getElementById("title").value;
	var status = document.getElementById("status").value;
	var url = "../controllers/updateRecommandInfo.php";
	var params = "recommandId=" + recommandId + "&title=" + title + "&status=" + status; 
	xmlhttp.open("POST",url,true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlhttp.onreadystatechange = updateRecommandInfoSuccess;
	xmlhttp.send(params);
}

function updateRecommandInfoSuccess()
{	
	if (xmlhttp.readyState == 4)
	if (xmlhttp.status == 200)
	{
		location.reload();
		alert("修改推荐信息成功");
	}
    else if (request.status == 404)
        alert("请求的链接不存在");
    else
        alert("错误: 错误代码 " + xmlhttp.status);
}

function refreshPage(recommandId,title,status)
{
	var temp = document.createElement("form");        
    temp.action = "../views/toefl_recommand_update.php";        
    temp.method = "post";        
    temp.style.display = "none";          
    var opt = document.createElement("textarea");        
    opt.name = "recommandId";        
    opt.value = recommandId;             
    temp.appendChild(opt); 
    
    
    var opt = document.createElement("textarea");        
    opt.name = "title";        
    opt.value = title;             
    temp.appendChild(opt);
    
    var opt = document.createElement("textarea");        
    opt.name = "status";        
    opt.value = status;             
    temp.appendChild(opt);
    document.body.appendChild(temp);        
    temp.submit();  
}

function gotoUpdateTopic(setId)
{
	var requestUrl = "../views/edittopic.php?setId=" + setId;
	window.location.href=requestUrl;
}

function changeMark(setId, recommandId, isImportant)
{
	getXmlHttpObject();
	var title = document.getElementById("title").value;
	var status = document.getElementById("status").value;
	var url = "../controllers/updateSetRemark.php";
	var params = "recommandId=" + recommandId + "&setId=" + setId + "&isImportant=" + isImportant; 
	xmlhttp.open("POST",url,true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlhttp.onreadystatechange = changeMarkSuccess;
	xmlhttp.send(params);
}

function changeMarkSuccess()
{	
	if (xmlhttp.readyState == 4)
	if (xmlhttp.status == 200)
	{	
		location.reload();
	}
    else if (request.status == 404)
        alert("请求的链接不存在");
    else
        alert("错误: 错误代码 " + xmlhttp.status);
}